//
//  FimilyMenberViewController.m
//  workApp
//
//  Created by 林 on 2019/1/12.
//  Copyright © 2019 林. All rights reserved.
//

#import "FimilyMenberViewController.h"
#import "FamilyMenberTableViewCell.h"
#import "AddFamilyViewController.h"
@interface FimilyMenberViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * tableV;
@property (nonatomic,strong)NSArray * dataArray;
@end

@implementation FimilyMenberViewController
- (void)getData{
    NSDictionary * dic = @{@"family_id":FamilyID};
[MDNetDetail GETNet:FamilyMenber_url param:dic block:^(id posts, NSInteger code, NSString *Meg) {
    NSLog(@"%@",posts);
    self.dataArray = posts[@"data"];
    [self.tableV reloadData];
}] ;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHexStr:@"#f7f7f7"];
    self.title = @"家庭成员管理";
    self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(10, 0, MDSCREEN_WIDTH - 20, MDSCREEN_HEIGHT - 100) style:(UITableViewStylePlain)];
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.showsHorizontalScrollIndicator = false;
    self.tableV.showsVerticalScrollIndicator = false;
    [self.view addSubview:self.tableV];
    
    UIButton * newBoxBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, MDSCREEN_HEIGHT - 80, MDSCREEN_WIDTH - 200, 50)];
    newBoxBtn.backgroundColor = [UIColor colorWithHexStr:@"#6fac45"];
    [newBoxBtn setTitle:@"添加家庭成员" forState:(UIControlStateNormal)];
    newBoxBtn.layer.cornerRadius = 25;
    newBoxBtn.layer.masksToBounds = true;
    [newBoxBtn addTarget:self action:@selector(addNewClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:newBoxBtn];
    [self getData];
}
- (void)addNewClick{
    AddFamilyViewController * addFamilyVC = [[AddFamilyViewController alloc]init];
    [self.navigationController pushViewController:addFamilyVC animated:true];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FamilyMenberTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[FamilyMenberTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (self.dataArray.count > 0) {
        NSDictionary * dic = self.dataArray[indexPath.row];
        cell.imageV.image = [UIImage imageNamed:@"head"];
        cell.nameLab.text = [NSString stringWithFormat:@"%@",dic[@"name"]];
        cell.phoneLab.text = [NSString stringWithFormat:@"%@",dic[@"phone"]];
        cell.relationLab.text =[NSString stringWithFormat:@"%@",dic[@"relationship"]];
    }
    return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return true;
}

-(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认删除吗" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                NSDictionary * dic = self.dataArray[indexPath.row];
                NSDictionary * ddd = @{@"member_id":dic[@"member_id"]};
                [MDNetDetail GETNet:DeleteFamilyMenber_url param:ddd block:^(id posts, NSInteger code, NSString *Meg) {
                    [MBProgressHUD showInfoMessage:Meg];
                    [self getData];
                }];
            }];
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertC addAction:cancelAction];
        [alertC addAction:sureAction];
        [self presentViewController:alertC animated:true completion:^{
        }];
    }];
    rowAction.backgroundColor = [UIColor colorWithHexStr:@"#7d7d7d"];
    NSArray *arr = @[rowAction];
    return arr;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
    if (indexPath.section == 2){
        
     }
    }
}
    

    
 

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
